package com.luajit.decompile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 调用反编译工具批量反编译luajit
 */
public class MyClassJit205 {
    public static  void  main(String args[]){

        findFolderFileList("D:\\Cocos2dDump\\lhcx");

    }

    /**
     *
     * @param path
     */
    public static void findFolderFileList(String path) {
        File file1 = new File(path);
        if (file1.exists()) {
            File[] files = file1.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println(file2.getAbsolutePath());
                        findFolderFileList(file2.getAbsolutePath());
                    } else {
                        try {

                            FileInputStream in = new FileInputStream(file2);
                            int tempbyte;
                            //1b4c4a12
                            int[] header = new int[4];
                            for (int i = 0; i < 4; i++) {
                                tempbyte = in.read();
                                header[i] = tempbyte;

                            }
                            String pathName =  file2.getAbsolutePath();
                            String fileName = pathName.replace("D:\\Cocos2dDump\\lhcx\\","")
                                    .replace(".luac",".lua").replace("\\",".");
                            if (header[0]==0x1b&&header[1]==0x4c&&header[2]==0x4a   ){



                                if (true){
                                    {
                                        //复制文件

                                        File test = new File("E:/Luajit/luajit-decomp-205/test.lua");
                                        FileInputStream testin = new FileInputStream(file2);
                                        FileOutputStream testout = new FileOutputStream(test);

                                        byte[] buffer = new byte[1024];
                                        int len;

                                        while ((len = testin.read(buffer)) > 0) {
                                            testout.write(buffer, 0, len);
                                        }
                                        testin.close();
                                        testout.close();
                                        System.out.println("复制文件luajit:"+pathName);
                                    }
                                    Thread.sleep(1*500);

                                    {

                                        Process p = Runtime.getRuntime().exec("cmd /c E: & cd Luajit/luajit-decomp-205 & decoder_new");
//                                C:\>E:
//                                E:\Luajit\luajit-decomp-205>
//                                E:\Luajit\luajit-decomp-205>decoder_new

                                    }
                                    Thread.sleep(1*500);
                                    {
                                        System.out.println("复制文件lua文件:"+fileName);

                                        File out2 = new File ("E:/Luajit/luajit-decomp-205/out2.lua");
                                        FileInputStream inputStream = new FileInputStream(out2);
                                        FileOutputStream outputStream = new FileOutputStream("dumplua/out/"+fileName);

                                        byte[] buffer = new byte[1024];
                                        int len;

                                        while ((len = inputStream.read(buffer)) > 0) {
                                            outputStream.write(buffer, 0, len);
                                        }
                                        inputStream.close();
                                        outputStream.close();

                                    }
                                }


                            }
                            else {

                                System.out.println("复制文件源文件:"+fileName);

                                FileInputStream inputStream = new FileInputStream(file2);
                                FileOutputStream outputStream = new FileOutputStream("dumplua/out/"+fileName);

                                byte[] buffer = new byte[1024];
                                int len;

                                while ((len = inputStream.read(buffer)) > 0) {
                                    outputStream.write(buffer, 0, len);
                                }
                                inputStream.close();
                                outputStream.close();
                            }
                            in.close();
                        }catch (Exception e){
                            System.out.println("error!!!"+e.getMessage());
                        }

                    }
                }
            }
        } else {
            System.out.println("error!!!");
        }
    }
}
